<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>编辑收货地址</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <link rel="stylesheet" type="text/css" href="$!webPath/resources/app/mcjd/css/common_jmc.css"/>
    <link rel="stylesheet" type="text/css" href="$!webPath/resources/app/mcjd/css/jmc_icon.css"/>
    <link rel="stylesheet" type="text/css" href="$!webPath/resources/app/mcjd/css/jmc_ufm.css"/>

    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        window._URI_ = {
            IP0: '$!mallBaseurl', // 本地
            IP1: 'http://testmall.yijiahn.com/mall' // 开发环境
        }
        window._URL_ = {
        	editAddr: _URI_.IP0 + '/app/user/address/save.json', // 新增地址
        	addressList: _URI_.IP0 + '/app/mcjd/addressList.htm' //地址列表
        };
    </script>
</head>
<body style="background:#fafafa;">

    <div id="bxTopbar">
        <div class="bx_topbar">
            <div class="h_44"></div>
            <div class="wp_topbar">
                <div class="it_btn it_back_btn" onclick="javascript:history.back(-1);"><i class="icon ico_1"></i></div>
                <a id="btnAddrSure" class="it_btn it_sure_btn" href="javascript:;">确定</a>
                <div class="it_toptt">编辑收货地址</div>
            </div>
        </div>
    </div>

    <div class="bx-addrfm">
        <div class="wp-addrfm">
            <ul class="ul_0 bg_fff ft30">
                <li class="li_0 li_0_t">
                    <div class="li_c">
                        <input id="t_name" type="text" placeholder="姓名" value="$!addressInfo.receiverName" />
                    </div>
                </li>
                <li class="li_0 li_0_t">
                    <div class="li_c">
                        <input id="t_phone" type="number" placeholder="联系电话" pattern="[0-9]*" value="$!addressInfo.mobile" />
                    </div>
                </li>
                <li class="li_0 li_0_select">
                    <div class="li_c li_c_select" id="selectAddr">
                    $!addressInfo.provinceName $!addressInfo.cityName $!item.countyName
                    </div><input id="v_selectAddr" type="hidden" value="$!addressInfo.provinceId,$!addressInfo.cityId,$!addressInfo.countyId"/>
                    <div class="li_r li_r_ico"></div>
                </li>
                <li class="li_0 li_0_select">
                    <div class="li_c li_c_select" id="selectStreet">
                    $!addressInfo.streetName
                    </div><input id="v_selectStreet" type="hidden" value="$!addressInfo.streetId"/>
                    <div class="li_r li_r_ico"></div>
                </li>
                <li class="li_0 li_0_t">
                    <div class="li_c">
                        <input id="t_addr" type="text" placeholder="请填写详情地址，精确至门牌号等" value="$!addressInfo.detailAddress" />
                    </div>
                </li>
            </ul>
        </div>

        <div class="wp-setdefault">
        	#if ($!addressInfo.isdefault == 1)
            <div id="btnSetDefault" class="btn_setdefault s__checked">设置为默认地址</div>
        	#else
            <div id="btnSetDefault" class="btn_setdefault">设置为默认地址</div>
        	#end
        </div>
    </div>

    <div class="overlay__bg" style="display:none;position:fixed;top:0;bottom:0;right:0;left:0;z-index:999999;background:rgba(255,255,255,0.15);"></div>

    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/touch.min.js"></script>
    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/utils.js"></script>
    <link rel="stylesheet" type="text/css" href="$!webPath/resources/app/mcjd/js/LArea.css"/>
    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/L_Select.js"></script>
    <script>
        $(function() {
        	var _accessToken = "$!accessToken",_orgId="$!orgId",_goodsCartIds="$!goodsCartIds",_addressId="$!addressId";
        	
            var data_3 = [{"areaId":4523655,"areaName":"河南省","children":[{"areaId":4523656,"areaName":"郑州市","children":[{"areaId":4523657,"areaName":"郑东新区","areaLevel":2},{"areaId":4523658,"areaName":"中原区","areaLevel":2},{"areaId":4523659,"areaName":"二七区","areaLevel":2},{"areaId":4523660,"areaName":"管城回族区","areaLevel":2},{"areaId":4523661,"areaName":"金水区","areaLevel":2},{"areaId":4523662,"areaName":"上街区","areaLevel":2},{"areaId":4523663,"areaName":"惠济区","areaLevel":2},{"areaId":4523664,"areaName":"中牟县","areaLevel":2},{"areaId":4523665,"areaName":"巩义市","areaLevel":2},{"areaId":4523666,"areaName":"荥阳市","areaLevel":2},{"areaId":4523667,"areaName":"新密市","areaLevel":2},{"areaId":4523668,"areaName":"新郑市","areaLevel":2},{"areaId":4523669,"areaName":"登封市","areaLevel":2},{"areaId":4577078,"areaName":"高新技术开发区","areaLevel":2},{"areaId":4577081,"areaName":"经济开发区","areaLevel":2}],"areaLevel":1},{"areaId":4581938,"areaName":"五家渠市","children":[{"areaId":4586871,"areaName":"五家渠市","areaLevel":2}],"areaLevel":1}],"areaLevel":0}];
            var data_1 = [{"areaId":4537797,"areaName":"南岭街道","areaLevel":3},{"areaId":4537798,"areaName":"自强街道","areaLevel":3},{"areaId":4537799,"areaName":"民康街道","areaLevel":3},{"areaId":4537800,"areaName":"新春街道","areaLevel":3},{"areaId":4537801,"areaName":"长通街道","areaLevel":3},{"areaId":4537802,"areaName":"全安街道","areaLevel":3},{"areaId":4537803,"areaName":"曙光街道","areaLevel":3},{"areaId":4537804,"areaName":"永吉街道","areaLevel":3},{"areaId":4537805,"areaName":"桃源街道","areaLevel":3},{"areaId":4537806,"areaName":"鸿城街道","areaLevel":3},{"areaId":4537807,"areaName":"明珠街道","areaLevel":3},{"areaId":4537815,"areaName":"幸福乡","areaLevel":3},{"areaId":4573176,"areaName":"我不清楚","areaLevel":3}];
            var area1 = new LSelect();
            area1.init({
                'trigger': '#selectAddr',
                'valueTo': '#v_selectAddr',
                'type': 1,
                'keys': {
                    id: 'areaId', name: 'areaName'
                },
                'data': data_3 || [],
                callbackFn: function(res) {
                    console.log(res)
                }
            });
            var area2 = new LSelect();
            area2.init({
                'trigger': '#selectStreet',
                'valueTo': '#v_selectStreet',
                'type': 1,
                'ncol': 1,
                'keys': {
                    id: 'areaId', name: 'areaName'
                },
                'data': data_1 || [],
                callbackFn: function(res) {
                    console.log(res)
                }
            });

            // 设置默认
            $('#btnSetDefault').on('tap', function(e) {
                if($(this).hasClass('s__checked')) {
                    $(this).removeClass('s__checked');
                }else  {
                    $(this).addClass('s__checked');
                }
            });
            // 确定
            $('#btnAddrSure').on('click', function(e) {
                var t_name = $('#t_name').val();
                var t_phone = $('#t_phone').val();
                var t_addr = $('#t_addr').val();
                var selectAddr = $('#selectAddr').val();
                var v_selectAddr = $('#v_selectAddr').val(); // 下拉选项 selectAddr、v_selectAddr都不为空才可以
                var selectStreet = $('#selectStreet').val();
                var v_selectStreet = $('#v_selectStreet').val(); // 下拉选项 selectStreet、v_selectStreet都不为空才可以
                
                var is_radio = 0;  // 是否默认地址（true 为选中默认地址）
                if($('#btnSetDefault').hasClass('s__checked')) {
                    is_radio = 1;
                }
                
                // 提交
                $('.overlay__bg').show(0);
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    timeout: 5000,
                    url: _URL_.editAddr,
                    headers: {},
                    data: {
                    	accessToken: _accessToken,
                    	orgId: _orgId,
                    	
                   		receiverName: t_name,
                       	mobile: t_phone,
                       	detailAddress: t_addr,
                       	countyId: "",
                       	addressId: "$!addressInfo.addressId",
                       	streetId: v_selectStreet,
                       	isdefault: is_radio,
                       	orgShipfree: 0,
                       	servicestationId: ""
                    },
                    success: function(res) {
                        $('.overlay__bg').hide(0);
                        if ("100" === res.code) { //请求成功
                        	location.href = _URL_.addressList +"?accessToken="+_accessToken +"&orgId="+_orgId +"&goodsCartIds="+_goodsCartIds +"&addressId="+_addressId;
                        } else if ("401" === res.code) { //App端请求，需要登录
                            Utils.showToast("请登录后再操作！");
                        } else if ("405" === res.code) { //微信端请求，需要登录
                            Utils.showToast("请先微信授权后再操作！");
                            WxUtils.goWxPage(window.location.href, 1000);
                        } else {
                            console.log('请求出错: ' + _URL_.goodsDetailById);
                            Utils.showToast(res.code + ":" + res.msg || '请求出错');
                        }
                    },
                    error: function(xhr) {
                        $('.overlay__bg').hide(0);
                        console.log('请求出错: ' + _URL_.goodsDetailById);
                        var msg = "请求出错(请检查相关网络状况)";
                        Utils.showToast(msg);
                    }
                });
            });
        })
    </script>

</body>
</html>